/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pp.persistance;

import com.pp.entites.Allocationid;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.pp.entites.Mobiledevice;
import com.pp.persistance.exceptions.NonexistentEntityException;
import com.pp.persistance.exceptions.PreexistingEntityException;
import com.pp.persistance.exceptions.RollbackFailureException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author arnaud
 */
public class AllocationidJpaController implements Serializable {

    public AllocationidJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Allocationid allocationid) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Mobiledevice idmobileDevice = allocationid.getIdmobileDevice();
            if (idmobileDevice != null) {
                idmobileDevice = em.getReference(idmobileDevice.getClass(), idmobileDevice.getId());
                allocationid.setIdmobileDevice(idmobileDevice);
            }
            em.persist(allocationid);
            if (idmobileDevice != null) {
                idmobileDevice.getAllocationidList().add(allocationid);
                idmobileDevice = em.merge(idmobileDevice);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findAllocationid(allocationid.getId()) != null) {
                throw new PreexistingEntityException("Allocationid " + allocationid + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Allocationid allocationid) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Allocationid persistentAllocationid = em.find(Allocationid.class, allocationid.getId());
            Mobiledevice idmobileDeviceOld = persistentAllocationid.getIdmobileDevice();
            Mobiledevice idmobileDeviceNew = allocationid.getIdmobileDevice();
            if (idmobileDeviceNew != null) {
                idmobileDeviceNew = em.getReference(idmobileDeviceNew.getClass(), idmobileDeviceNew.getId());
                allocationid.setIdmobileDevice(idmobileDeviceNew);
            }
            allocationid = em.merge(allocationid);
            if (idmobileDeviceOld != null && !idmobileDeviceOld.equals(idmobileDeviceNew)) {
                idmobileDeviceOld.getAllocationidList().remove(allocationid);
                idmobileDeviceOld = em.merge(idmobileDeviceOld);
            }
            if (idmobileDeviceNew != null && !idmobileDeviceNew.equals(idmobileDeviceOld)) {
                idmobileDeviceNew.getAllocationidList().add(allocationid);
                idmobileDeviceNew = em.merge(idmobileDeviceNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = allocationid.getId();
                if (findAllocationid(id) == null) {
                    throw new NonexistentEntityException("The allocationid with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Allocationid allocationid;
            try {
                allocationid = em.getReference(Allocationid.class, id);
                allocationid.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The allocationid with id " + id + " no longer exists.", enfe);
            }
            Mobiledevice idmobileDevice = allocationid.getIdmobileDevice();
            if (idmobileDevice != null) {
                idmobileDevice.getAllocationidList().remove(allocationid);
                idmobileDevice = em.merge(idmobileDevice);
            }
            em.remove(allocationid);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Allocationid> findAllocationidEntities() {
        return findAllocationidEntities(true, -1, -1);
    }

    public List<Allocationid> findAllocationidEntities(int maxResults, int firstResult) {
        return findAllocationidEntities(false, maxResults, firstResult);
    }

    private List<Allocationid> findAllocationidEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Allocationid.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Allocationid findAllocationid(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Allocationid.class, id);
        } finally {
            em.close();
        }
    }

    public int getAllocationidCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Allocationid> rt = cq.from(Allocationid.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
